﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Oracle.DataAccess.Client;

namespace WoBian.DA
{
    /// <summary>
    /// 数据库帮助类
    /// </summary>
    public class DataAccessBase
    {
        protected readonly IDbConnection m_Connection;
        protected readonly IDbTransaction m_Transaction;

        public DataAccessBase(IDbConnection aConnection)
        {
            m_Connection = aConnection;
        }

        public DataAccessBase(IDbTransaction aTransaction)
        {
            m_Connection = aTransaction.Connection;
            m_Transaction = aTransaction;
        }

        public IDataReader ExecuteReader(string aSql, IEnumerable<IDataParameter> aParameters = null, CommandType aCommandType = CommandType.Text)
        {
            IDbCommand cmd = m_Connection.CreateCommand();
            if (m_Transaction != null)
            {
                cmd.Transaction = m_Transaction;
            }
            cmd.CommandText = aSql;
            cmd.CommandType = aCommandType;
            if (aParameters != null)
            {
                cmd.AddParameters(aParameters);
            }
            return cmd.ExecuteReader();
        }

        public int ExecuteNonQuery(string aSql, IEnumerable<IDataParameter> aParameters = null, CommandType aCommandType = CommandType.Text)
        {
            IDbCommand cmd = m_Connection.CreateCommand();
            if (m_Transaction != null)
            {
                cmd.Transaction = m_Transaction;
            }
            cmd.CommandText = aSql;
            cmd.CommandType = aCommandType;
            if (aParameters != null)
            {
                cmd.AddParameters(aParameters);
            }
            return cmd.ExecuteNonQuery();
        }

        public object ExecuteScaler(string aSql, IEnumerable<IDataParameter> aParameters = null, CommandType aCommandType = CommandType.Text)
        {
            IDbCommand cmd = m_Connection.CreateCommand();
            if (m_Transaction != null)
            {
                cmd.Transaction = m_Transaction;
            }
            cmd.CommandText = aSql;
            cmd.CommandType = aCommandType;
            if (aParameters != null)
            {
                cmd.AddParameters(aParameters);
            }
            return cmd.ExecuteScalar();
        }

        public DataAccessBase ConvertTo(Type aType)
        {
            DataAccessBase dataAccessBase;
            if (m_Transaction != null)
            {
                dataAccessBase = Activator.CreateInstance(aType, m_Transaction) as DataAccessBase;
            }
            else
            {
                dataAccessBase = Activator.CreateInstance(aType, m_Connection) as DataAccessBase;
            }
            return dataAccessBase;
        }

        public IDataParameter AddParameter(string parameterName, object pValue, DbType aType)
        {
            IDataParameter parameter = m_Connection.CreateCommand().CreateParameter();
            parameter.DbType = aType;
            parameter.ParameterName = parameterName;
            parameter.Value = pValue;
            return parameter;
        }

        public DataRow ExecuteDataRow(string aSql, IEnumerable<IDataParameter> aParameters = null, CommandType aCommandType = CommandType.Text)
        {
            IDbCommand cmd = m_Connection.CreateCommand();
            if (m_Transaction != null)
            {
                cmd.Transaction = m_Transaction;
            }
            cmd.CommandText = aSql;
            cmd.CommandType = aCommandType;
            if (aParameters != null)
            {
                cmd.AddParameters(aParameters);
            }
            IDataAdapter da = DataAccessFactory.CreateDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            if (ds != null)
                return ds.Tables[0].Rows[0];
            return null;

        }
        public DataTable ExecuteDataTable(string aSql, IEnumerable<IDataParameter> aParameters = null, CommandType aCommandType = CommandType.Text)
        {
            IDbCommand cmd = m_Connection.CreateCommand();
            if (m_Transaction != null)
            {
                cmd.Transaction = m_Transaction;
            }
            cmd.CommandText = aSql;
            cmd.CommandType = aCommandType;
            if (aParameters != null)
            {
                cmd.AddParameters(aParameters);
            }
            IDataAdapter da = DataAccessFactory.CreateDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds.Tables[0];
        }

        public DataSet ExecuteDataSet(string aSql, IEnumerable<IDataParameter> aParameters = null, CommandType aCommandType = CommandType.Text)
        {
            IDbCommand cmd = m_Connection.CreateCommand();
            if (m_Transaction != null)
            {
                cmd.Transaction = m_Transaction;
            }
            cmd.CommandText = aSql;
            cmd.CommandType = aCommandType;
            if (aParameters != null)
            {
                cmd.AddParameters(aParameters);
            }
            IDataAdapter da = DataAccessFactory.CreateDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds;
        }
    }
}
